package cn.ds.assets.module.assets.service.metadataModelBase;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.metadataModelBase.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataModelBase.BaseMetadataModelBaseDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.metadataModelBase.BaseMetadataModelBaseConvert;
import cn.ds.assets.module.assets.dal.mysql.metadataModelBase.BaseMetadataModelBaseMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 元数据管理-元模型-基础属性 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseMetadataModelBaseServiceImpl implements BaseMetadataModelBaseService {

    @Resource
    private BaseMetadataModelBaseMapper baseMetadataModelBaseMapper;

    @Override
    public Long createBaseMetadataModelBase(BaseMetadataModelBaseCreateReqVO createReqVO) {
        // 插入
        BaseMetadataModelBaseDO baseMetadataModelBase = BaseMetadataModelBaseConvert.INSTANCE.convert(createReqVO);
        baseMetadataModelBaseMapper.insert(baseMetadataModelBase);
        // 返回
        return baseMetadataModelBase.getId();
    }

    @Override
    public void updateBaseMetadataModelBase(BaseMetadataModelBaseUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseMetadataModelBaseExists(updateReqVO.getId());
        // 更新
        BaseMetadataModelBaseDO updateObj = BaseMetadataModelBaseConvert.INSTANCE.convert(updateReqVO);
        baseMetadataModelBaseMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseMetadataModelBase(Long id) {
        // 校验是否存在
        validateBaseMetadataModelBaseExists(id);
        // 逻辑删除
        baseMetadataModelBaseMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseMetadataModelBaseExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseMetadataModelBaseDO dataDo = baseMetadataModelBaseMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseMetadataModelBaseDO getBaseMetadataModelBase(Long id) {
        return baseMetadataModelBaseMapper.selectById(id);
    }

    @Override
    public List<BaseMetadataModelBaseDO> getBaseMetadataModelBaseList(Collection<Long> ids) {
        return baseMetadataModelBaseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseMetadataModelBaseDO> getBaseMetadataModelBasePage(BaseMetadataModelBasePageReqVO pageReqVO) {
        return baseMetadataModelBaseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseMetadataModelBaseDO> getBaseMetadataModelBaseList(BaseMetadataModelBaseExportReqVO exportReqVO) {
        return baseMetadataModelBaseMapper.selectList(exportReqVO);
    }

}
